/**
 * 
 */
package com.gmail.blackdog1987.carcare.activity;

import java.util.Arrays;
import java.util.List;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.gmail.blackdog1987.carcare.App;
import com.gmail.blackdog1987.carcare.BaseActivity;
import com.gmail.blackdog1987.carcare.R;
import com.gmail.blackdog1987.carcare.entity.TGasConfig;
import com.gmail.blackdog1987.carcare.network.request.builder.GasGetConfigRequestBuilder;
import com.gmail.blackdog1987.carcare.network.request.builder.GasValidCardRequestBuilder;
import com.gmail.blackdog1987.carcare.network.response.GasGetConfigResponse;
import com.gmail.blackdog1987.carcare.network.response.GasValidCardResponse;
import com.gmail.blackdog1987.carcare.network.response.handler.GasGetConfigResponseHandler;
import com.gmail.blackdog1987.carcare.network.response.handler.GasValidCardResponseHandler;
import com.gmail.blackdog1987.carcare.network.response.handler.IResponseHandler;
import com.gmail.blackdog1987.carcare.network.response.handler.OnResponseListener;

public class GasActivity extends BaseActivity implements OnClickListener, OnResponseListener {
	private Context mContext;
	
	AlertDialog mCardPayDialog;
	GasValidCardRequestBuilder mReq = new GasValidCardRequestBuilder();
	GasValidCardResponseHandler mHandler = new GasValidCardResponseHandler();
	
	GasGetConfigRequestBuilder mConfigReq = new GasGetConfigRequestBuilder();
	GasGetConfigResponseHandler mConfigHandler = new GasGetConfigResponseHandler();
	
	TextView price90;
	TextView price93;
	TextView price97;
	TextView price0;
	
	public static List<TGasConfig> configs;

	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_gas);
		mContext = this;
		initViews();
		initData(savedInstanceState);
		reg();
	}

	public void initViews() {
		price90 = (TextView) findViewById(R.id.gas_price_90);
		price93 = (TextView) findViewById(R.id.gas_price_93);
		price97 = (TextView) findViewById(R.id.gas_price_97);
		price0 = (TextView) findViewById(R.id.gas_price_0);
	}

	public void reg() {
		findViewById(R.id.do_gas_card).setOnClickListener(this);
		findViewById(R.id.do_pay_gas_card).setOnClickListener(this);
		findViewById(R.id.near_gas_statution).setOnClickListener(this);
		findViewById(R.id.back).setOnClickListener(this);
		mHandler.setOnResponseListener(this);
	}

	public void initData(Bundle savedInstanceState) {
		mConfigHandler.setOnResponseListener(this);
		mConfigReq.postJsonRequest(mContext, mConfigHandler);
		
		price90.setText(App.GAS_PRICE_MAP.get("90"));
		price93.setText(App.GAS_PRICE_MAP.get("93"));
		price97.setText(App.GAS_PRICE_MAP.get("97"));
		price0.setText(App.GAS_PRICE_MAP.get("0"));
		
	}

	@Override
	public void onClick(View v) {

		switch (v.getId()) {
		case R.id.back:
			finish();
			break;
		case R.id.do_gas_card:
			startActivity(new Intent(this, GasCardActivity.class));
			break;
		case R.id.do_pay_gas_card:
			if(mCardPayDialog==null){
				View customView = LayoutInflater.from(mContext).inflate(R.layout.widget_gas_pay_input, null);
				final View ok = customView.findViewById(R.id.widget_gascardpay_ok);
				final EditText et = (EditText) customView.findViewById(R.id.widget_gascardpay_input);
				ok.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View v) {
						mCardPayDialog.dismiss();
						mReq.setCode(et.getText().toString()).postJsonRequest(mContext, mHandler);
					}
				});
				mCardPayDialog = new AlertDialog.Builder(mContext).setView(customView).setTitle("温馨提示").create();
			}
			mCardPayDialog.show();
			break;
		case R.id.near_gas_statution:
			startActivity(new Intent(this, GasStationNearActivity.class));
			break;
		default:
			break;
		}

	}

	@Override
	public void onResponse(IResponseHandler classz) {
		if(classz.getResponse() instanceof GasValidCardResponse){
			Intent i = new Intent(this, GasCardPayActivity.class);
			i.putExtra("data", ((GasValidCardResponse)classz.getResponse()).card);
			startActivity(i);
		}else if(classz.getResponse() instanceof GasGetConfigResponse){
			List<TGasConfig> configs = ((GasGetConfigResponse) classz.getResponse()).configs;
			for (TGasConfig tGasConfig : configs) {
				if(tGasConfig.getKey().equals("gas_price")){
					try {
						JSONObject json = new JSONObject(tGasConfig.getValue());
						App.GAS_PRICE_MAP.put("90", json.optString("90"));
						App.GAS_PRICE_MAP.put("93", json.optString("93"));
						App.GAS_PRICE_MAP.put("97", json.optString("97"));
						App.GAS_PRICE_MAP.put("0", json.optString("0"));
					} catch (JSONException e) {
						e.printStackTrace();
					}
				}
				
				if(tGasConfig.getKey().equals("charge_values")){
					try {
						JSONObject json = new JSONObject(tGasConfig.getValue());
						App.CHARGE_VALUE_MAP.put("create", Arrays.asList(json.optString("create").split(",")));
						App.CHARGE_VALUE_MAP.put("charge", Arrays.asList(json.optString("charge").split(",")));
					} catch (JSONException e) {
						e.printStackTrace();
					}
				}
			}
		}
	}

	@Override
	public void onError(IResponseHandler classz) {
		if(classz.getResponse() instanceof GasValidCardResponse){
			Toast.makeText(mContext, "验证失败，请检查卡号", Toast.LENGTH_LONG).show();
		}
		
	}
}
